package com.lxl.securitypermission.controller;

import cn.hutool.core.util.StrUtil;
import com.lxl.securitypermission.constant.HttpCode;
import com.lxl.securitypermission.dto.ResponseResult;
import com.lxl.securitypermission.service.UserService;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.ResponseBody;

/**
 * @author 绫小路
 * @date 2020/12/30 0:12
 * @description
 */
@Controller
public class RegisterController {

  @Autowired
  private UserService userService;


  @ApiOperation("注册页面")
  @GetMapping("register")
  public String register() {
    return "register";
  }

  @ApiOperation("注册账号")
  @PostMapping("register")
  @ResponseBody
  public ResponseResult register(String username, String password, String nickname, Integer sex) {
    if (StrUtil.isBlank(username)) {
      return new ResponseResult(HttpCode.FAIL, "账号不能为空！");
    }
    if (username.length() < 4) {
      return new ResponseResult(HttpCode.FAIL, "账号长度不能小于4 ！");
    }
    if (StrUtil.isBlank(password)) {
      return new ResponseResult(HttpCode.FAIL, "密码不能为空！");
    }
    if (StrUtil.isBlank(nickname)) {
      return new ResponseResult(HttpCode.FAIL, "昵称不能为空！");
    }
    if (sex == null) {
      return new ResponseResult(HttpCode.FAIL, "性别不能为空！");
    }
    return userService.register(username, password, nickname, sex);
  }
}
